package com.easypay;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.easypay.bean.*;
import com.easypay.bean.micropay.customerUnPay.AppendReqData;
import com.easypay.bean.micropay.customerUnPay.DataBean;
import com.easypay.bean.micropay.customerUnPay.UnPay;
import com.easypay.util.AesUtil;
import com.easypay.util.HttpConnectUtils;
import com.easypay.util.RandomUtil;
import com.easypay.util.StringUtils;

import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 移动支付DEMO, 对应微收单优化接口 https://doc.eycard.cn/web/#/285
 *
 * @author pandans
 */
public class MobilePayment extends BasePay{


    /**  微收单优化  */
    public static void main(String[] args) {

        //js pay
//        doJsapi();

        // 付款码
         doScanPay();

        // 主扫   native
        doNativePay();

        // 查询订单，请修改代码中订单号
//        doQuery();

        // 银联云闪付app支付（原手机控件支付）
//        doUnPay();

        // 获取云闪付用户授权信息
        //doYsfQrAuth();

        // 闪付获取用户标识
        //doQrGetUserId();

    }


    /**
     * 银联云闪付app支付（原手机控件支付）
     *
     * 请求报文为：{"appendData":{},"data":{"infoAttach":"商品描述","orderInfo":"云闪付app测试","patnerSettleFlag":"0","splitSettleFlag":"0","tradeAmt":"1"},"orgId":"ORG000000000007","orgMercode":"831135358120001","orgTermno":"21120433","orgTrace":"ORG0000720220104040301391231","sign":"Hh/k99dUgHLzU6VQW/PF+dRjdp9Ke0w4rxdMDaOjraJl2UrqVFwvzJBkeQ0w034AzBf19PvrCgNXSX6+2LfwqhpIq1nTqJ8Q6NcymrZ56pwN7sM55VWXTXTh7soD3oH932CmNXsEzIlbf7UgleTCCPkuQUSzSkxsz6FDf5aOGGo=","signType":"RSA2"}
     *
     * 响应报文：{"sign":"fkinH1OY1dpvfqzxCnNP3I0IG6Mbj59yCsMlRcgJXz8gkrExdqIf0feCPBaaZCObFJp5Uqy9fSz5Jbqzn9VlMRq2xuhWocY8FgIrmwwRINwJrkC05iSiPoaC5GBqShX2hOE/ehKWh22s810wwQwAvFFoIKD2+oj97W/s+6BEmE0=","data":{"finRetcode":"99","appendRetcode":"00","appendRetmsg":"成功[0000000]","tradeCode":"WUSDK","tradeAmt":"1","prepayId":"653302684552403399611","outTrace":"4101478275378928832512"},"sysRetcode":"000000","sysRetmsg":"success"}
     *
     */
    private static void doUnPay(){
        UnPay request = new UnPay();
        request.setOrgId(ORGID);
        request.setOrgMercode(ORGMERCODE);
        request.setOrgTermno(ORGTERMNO);
        request.setOrgTrace(ORGID.substring(0, 4) + ORGID.substring(ORGID.length() - 4) + StringUtils.getLocalDate() + RandomUtil.generateNumString(6));

        DataBean data = new DataBean();
        data.setOrderInfo("云闪付app测试");
        data.setInfoAttach("商品描述");
        data.setTradeAmt("1");

        data.setPatnerSettleFlag("0");
        data.setSplitSettleFlag("0");
        request.setData(data);

        // 使用商户私钥对data字段加签
        String sign = sign(data, TEST_MER_RSA_PRIVATE_KEY);
        request.setSign(sign);
        request.setSignType(SIGN_TYPE_RSA256);

        //request.setProdTrace("");
        AppendReqData appendReqData = new AppendReqData();
//        appendReqData.setOrgBasestation("");
//        appendReqData.setOrgDeviceIp("");
//        Identity identity = new Identity();
//        identity.setEncryptInfo("");
//        identity.setFixBuyer("");
//        identity.setType("");
//        identity.setMinage("");
//        identity.setNeedCheckInfo("");
//        appendReqData.setIdentity(identity);
        request.setAppendData(appendReqData);

        String requestStr = JSON.toJSONString(request);

        try {
            String response = HttpConnectUtils.sendHttpSRequest(BASE_URL + "/standard/app", requestStr, "JSON", null);
//            String response = HttpConnectUtils.sendHttpSRequest("http://192.168.3.24:12000" + "/standard/app", requestStr, "JSON", null);
            System.out.println("响应报文：" + response);
            checkSign(response);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * @Description 聚合支付- 标准-付款码支付 被扫，商户使用扫码枪等扫码设备读取用户二维码支付
     * @date: 2021/10/16 18:56
     * @author: pandans
     * 请求实例：
     * {"data":{"authCode":"135670896147652543","orderInfo":"微信被扫订单","timeoutMinutes":0,"tradeAmt":1},"orgId":"ORG000000000053","orgMercode":"531000012972310","orgTermno":"W1489510","orgTrace":"ORG0005320211019095744897746","sign":"2VP3Zu2f6s+AjgRhrdjDUDbjY8FRr4sn0Y2+S7N+Nf+Btr3KLKpeUVqL/gf/jX+kCqZHm2lIGfl27ccZDXDSana58wEIUsSCKHiDFzaNRMEhUNNY1/5WtP97qgvRJSnSIB6FpFOKedJOP92Yy0NGVQafDt4e0CQR6jkN+dHWKu0=","signType":"SHA256withRSA"}
     */
    public static void doScanPay() {
        ScanPay request = new ScanPay();
        request.setSignType(SIGN_TYPE_RSA256);
        request.setOrgId(ORGID);
        request.setOrgMercode(ORGMERCODE);
        request.setOrgTermno(ORGTERMNO);
        request.setOrgTrace(ORGID.substring(0, 4) + ORGID.substring(ORGID.length() - 4) + StringUtils.getLocalDate() + RandomUtil.generateNumString(6));


        //============================支付宝需要增加下面======================================

//        ScanPay.AppendDataBean appendData = new ScanPay.AppendDataBean();
//
//        TerminalInfo terminalInfo = new TerminalInfo();
//        terminalInfo.setLocation("+37.12/-121.213");
////
//        terminalInfo.setTerminalType("11");
//        terminalInfo.setSerialNum("00104206TCA8B3551");
////        terminalInfo.setTerminalIp("192.168.2.1");
//        terminalInfo.setTerminalId("32774788");
//
//        appendData.setTerminalinfo(terminalInfo);
////
//        request.setAppendData(appendData);
        //==================================================================

        //============================微信 ip location 加不加 都可以===========================

        ScanPay.AppendDataBean appendData = new ScanPay.AppendDataBean();

        TerminalInfo terminalInfo = new TerminalInfo();
        terminalInfo.setLocation("+37.12/-121.213");

        appendData.setTerminalinfo(terminalInfo);

        request.setAppendData(appendData);
        //==================================================================


        ScanPay.DataBean dataBean = new ScanPay.DataBean();
        //支付条码；长度：50 -- 付款码, 测试方法：微信支付宝，云闪付付款码然后点击上面轻触可以查看收款码数字
        // 银联需要  到业务文档-银联业务-被扫 找到条码信息

        dataBean.setAuthCode("289194550821419526");
//        dataBean.setAuthCode("281690732327669144");

        // 金额,单位分；取值范围：1-0000000000
        dataBean.setTradeAmt(1);
        dataBean.setOrderInfo("测试");
//        dataBean.setSplitSettleFlag("1");  // 分账：1
        request.setData(dataBean);


        // 使用商户私钥对data字段加签
        String sign = sign(dataBean, TEST_MER_RSA_PRIVATE_KEY);
        request.setSign(sign);

        String requestStr = JSON.toJSONString(request);

        try {
            String response = HttpConnectUtils.sendHttpSRequest(BASE_URL + "/standard/scanPay", requestStr, "JSON", null);
            System.out.println("\n响应报文：" + response);
            checkSign(response);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * @Description 聚合支付- 标准-JSAPI支付  小程序，公众号、服务窗场景,自行封装码牌功能使用。
     * @date: 2021/10/16 18:55
     * @author: pandans
     * 请求：{"data":{"orderInfo":"测试jsapi10分","orgBackUrl":"https://t-wapi.bhecard.com:1443/selfNotice/backNotice","payerId":"oIcV7wwvKsNAxdnTNhg2ylI4Oibo","timeoutMinutes":300,"tradeAmt":10,"tradeCode":"WTJS1"},"orgId":"ORG000000002856","orgMercode":"531000012972421","orgTermno":"W1489541","orgTrace":"ORG0285620211101040500221248","sign":"iliA+MTW1ZcI3dUwSd5gagz1iRd77vFk4HzWuhdbwVMFhb1dPNOTb/R18nLTCXOt6P4WeNb0GDi8fF2e34FHMxNaAdJR7RovXJUmiWFAwAFFBXHkzLLN1CYvPeW/wKDGaxcDQAPcplgzzdLwWUlw3/DuSZCVd+h7B3dKuzVSYxM=","signType":"RSA2"}
     * 返回：{"sign":"V3Hqeg08RmO74zni0Zb4k/rWsHH0taKLWWHUUvzocHTwoXlIxa5y+zSVzLlySWdqXi8f+wmlAqwCR7DQPxJZ65NBew0GDOwcj/u85iOlCofvo0T/J+PJb13iCIaEt5DuZYvl5t66BpqKGAd+qjoKwwM2otwJRx2B2mHNhbqgYMc=","data":{"finRetcode":"99","tradeCode":"WTJS1","wxWcPayData":"{\"appId\":\"wx2421b1c4370ec43b\",\"timeStamp\":\"1635753902\",\"nonceStr\":\"b9a0b8bad37f4d438fe42e0c40bbf560\",\"package\":\"prepay_id=wx011605022911839cc6dade19e3c77b0000\",\"signType\":\"RSA\",\"paySign\":\"rlA7PgjWY+aw/Tdmuqgxgf7hHEJ91fG2JZ+xwz0qrOIyMGVNdoO1LOzJlmsCQL9n6xQ4Nrvpji3AsjeWDbGF9io9IDfnWe6dNK2iI7/7UHq+pyfEcnF/wtF7CimKS4eRNHweU799d0gFi5XSYhpJa+ItgoaPX2vIaZJEsOLSjxkQiqi2Nu7g6W+WlJJyV4t9ahWNOLt8JovmgWnzyAcHxySJtmjv80SiMc7GtgJwO0psBzRujPNnWff5/snj/4brAZasoPJtFDrLRYv4zk0jJu4RZ6YDZCsnkLbJm/sphhjm3sBvSjo4itJs+LXNBzhN8jS/rT00NUq4/oStJHvWuw==\"}","wxPrepayId":"wx011605022911839cc6dade19e3c77b0000","outTrace":"1121455082392221011968"},"sysRetcode":"000000","sysRetmsg":"success"}
     */
    public static void doJsapi() {

        JsApi.DataBean dataBean = new JsApi.DataBean();
        dataBean.setOrgFrontUrl("https://www.baidu.com");
        dataBean.setOrgBackUrl("https://t-wapi.bhecard.com:1443/selfNotice/backNotice");

        // tradeCode 只有  WAJS1:支付宝-生活号支付;WAJS2:支付宝-小程序支付;WTJS1:微信-公众号支付;WTJS2:微信-小程序支付;WUJS1:银联二维码-JS支付(payerId 传入 仿真app的 用户名，比如admin4)   这几种选择

        dataBean.setTradeCode("WUJS1");
        // 金额,单位分；取值范围：1-10000000000

        dataBean.setPayerId("admin4");
//        dataBean.setPayerId("oLiz35ZQJb3e5BcK0RivX4y6qIt8");
//        dataBean.setPayerId("2088102936140877");  // 这个可以
//        dataBean.setPayerId("o8As_44aPtWNiwFg1PrtAT0lsVlQ");


        // payerId 和 wxSubAppid 一定要匹配, 这是一对儿

//        dataBean.setPayerId("2088612409652625"); // 凯杰 支付宝
//        dataBean.setWxSubAppid("wxde94a63529b9b709");
//
//        dataBean.setPayerId("oIcV7w-8iHNq8ERvqikH1YiBxQe4");  // 凯杰 微信
//        dataBean.setWxSubAppid("wxde94a63529b9b709");         // 凯杰 微信
//
//        dataBean.setPayerId("oVoLCwZzRVHwPrG3JvDs5Z_yc83s");  //
//        dataBean.setWxSubAppid("wxee597080118d5c2d");         // 这个应该是 数据库配置的那
//
//        dataBean.setPayerId("oUpF8uO-kIpPRDvFevZCerLc-ipo");  // 259改造 （微信）
//        dataBean.setWxSubAppid("wxee597080118d5c2d");         // 259改造 （微信）
////
//        dataBean.setPayerId("2088402489184084");            // 259改造 （支付宝）
//        dataBean.setWxSubAppid("1266000048310000");         // 259改造 （支付宝）


        //订单信息（订单明细内容，如订单标题、订单描述等）
        dataBean.setOrderInfo("测试jsapi");
        dataBean.setTradeAmt(1);
        //订单支付超时时间；长度：8。单位：秒, 不填默认为300（5分钟)
        dataBean.setTimeoutMinutes(300);




        JsApi.AppendDataBean appendData = new JsApi.AppendDataBean();
        JsApi.AppendDataBean.IdentityBean identityBean = new JsApi.AppendDataBean.IdentityBean();
        // 传递脱敏信息
//        String aesKey = "dfuncyf3rlum9vzwxu5iw3dyx55iykle";
        String aesKey = "ASDEFC2905095917frtghy8289072487";
        JSONObject encryInfo = new JSONObject();
        // 手机号可不填
        encryInfo.put("mobile", "手机号");
        encryInfo.put("name", "姓名");
        encryInfo.put("number", "身份证");
//        System.out.println(encryInfo.toJSONString());
        identityBean.setEncryptInfo(AesUtil.encrypt(encryInfo.toJSONString(), aesKey));

        identityBean.setFixBuyer("T");
        identityBean.setMinage("18");
        identityBean.setType("01");
//        identityBean.setNeedCheckInfo("T");
        appendData.setDgtlEnvlp(AesUtil.dgtlEnvelp(aesKey, EASYPAY_PUBLIC_KEY));
        appendData.setIdentity(identityBean);
//        appendData.setAliDiscountableAmount(8);
//        appendData.setAliUndiscountableAmount(2);


        //========================= 微信 加不加 ip 和 location 都可以 =======================
        TerminalInfo terminalInfo = new TerminalInfo();
        terminalInfo.setLocation("+37.12/-121.213");
        appendData.setTerminalinfo(terminalInfo);
        //=====================================================================








        JsApi request = new JsApi();
        request.setOrgId(ORGID);
        request.setOrgMercode(ORGMERCODE);
        request.setOrgTermno(ORGTERMNO);
        request.setSignType(SIGN_TYPE_RSA256);
        // 交易流水，28位定长，规则：orgId 前四位+后四位+yyyyMMddhhmmss+6位自定义 合计28位长度（唯一）
        request.setOrgTrace(ORGID.substring(0, 4) + ORGID.substring(ORGID.length() - 4) + StringUtils.getLocalDate() + RandomUtil.generateNumString(6));

        request.setAppendData(appendData);

        request.setData(dataBean);
        // 使用商户私钥对data字段加签
        String sign = sign(dataBean, TEST_MER_RSA_PRIVATE_KEY);
        request.setSign(sign);

        String requestStr = JSON.toJSONString(request);

        System.out.println("反反复复：" + requestStr);

        try {
            String response = HttpConnectUtils.sendHttpSRequest(BASE_URL + "/standard/jsapi", requestStr, "JSON", null);
            System.out.println("\n响应报文：" + response);
            checkSign(response);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /**
     * qrUserAuthCode、qrAppUpIdentifier 云闪付执行qrAuth get方法获取的用户appupidentifier
     * @Description 云闪付获取用户标识
     * @date: 2021/10/14 23:20
     * @author: pandans
     */
    public static void doQrGetUserId() {
        UnionUserId request = new UnionUserId();
        request.setOrgId(ORGID);
        request.setOrgMercode(ORGMERCODE);
        request.setOrgTermno(ORGTERMNO);
        request.setSignType(SIGN_TYPE_RSA256);
        // 交易流水，28位定长，规则：orgId 前四位+后四位+yyyyMMddhhmmss+6位自定义 合计28位长度（唯一）
        request.setOrgTrace(ORGID.substring(0, 4) + ORGID.substring(ORGID.length() - 4) + StringUtils.getLocalDate() + RandomUtil.generateNumString(6));

        UnionUserId.DataBean dataBean = new UnionUserId.DataBean();
        //授权码（付款方返回的临时授权码，一次有效）测试方法：微信支付宝，云闪付付款码然后点击上面轻触可以查看收款码数字
        dataBean.setQrUserAuthCode("");//6226176223328162414
        dataBean.setQrAppUpIdentifier("");

        request.setData(dataBean);
        // 使用商户私钥对data字段加签
        String sign = sign(dataBean, TEST_MER_RSA_PRIVATE_KEY);
        request.setSign(sign);

        String requestStr = JSON.toJSONString(request);

        try {
            String response = HttpConnectUtils.sendHttpSRequest(BASE_URL + "/userId/qrGetUserId", requestStr, "JSON", null);
            System.out.println("\n响应报文：" + response);
            checkSign(response);

            // 云闪付用户id
            JSONObject rep = JSON.parseObject(response);
            if (RET_CODE_SUCCESS.equals(rep.getString("sysRetcode"))) {
                String payerId = rep.getJSONObject("data").getString("payerId");
                System.out.println("云闪付开放ID:" + payerId);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 云闪付获取授权码  ！！是获取opneid的前置条件
     */
    public static String doYsfQrAuth() {
        // 1. 使用get方法获取云闪付的零时用户授权

        // 重定向地址（自己服务器地址，用于接收授权码）要提前在 银联加白名单 且只能加生产的。
        String redirectUrl = "https://www.baidu.com";
        String url = BASE_URL + "/userId/qrAuth?qrRedirectUrl=" + "redirectUrl";

        // 2. 如上url在云闪付中执行， 云闪付会自动重定向到 redirectUrl，然后商户自行根据接口文档步骤获取到appupidentifier
        // 从HttpRequest获取参数，如用Springboot中， notify(HttpServletRequest request, HttpServletResponse response)
        // String userAgent = request.getHeader("user-agent");
        // 正式调用应该是再服务上获取 redirectUrl 对应的request， 此处直接模拟
        String userAgent = "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Mobile Safari/ UnionPay/1.0 TESTPAY";
        String uaReg = ".*(UnionPay/[^ ]+ ([0-9a-zA-Z]+)).*";
        Pattern p = Pattern.compile(uaReg);
        Matcher m = p.matcher(userAgent);
        if (!m.matches()) {
            System.out.println("非银联云闪付APP。");
            return "";
        }
        String appUpIdentifier = m.group(1);
        // 3. appupidentifier 仅仅一次使用，过期请重复上面1，2。 该appupidentifier 用于获取云闪付的userid
        return appUpIdentifier;
    }

    /**
     * @Description 主扫支付 （微收单优化， 不支持 微信主扫 ）
     * @date: 2021/10/16 18:53
     * @author: pandans
     */
    public static void doNativePay() {
        NativePay request = new NativePay();
        request.setOrgId(ORGID);
        request.setOrgMercode(ORGMERCODE);
        request.setOrgTermno(ORGTERMNO);
        request.setSignType(SIGN_TYPE_RSA256);

        // 交易流水，28位定长，规则：orgId 前四位+后四位+yyyyMMddhhmmss+6位自定义 合计28位长度（唯一）
        request.setOrgTrace(ORGID.substring(0, 4) + ORGID.substring(ORGID.length() - 4) + StringUtils.getLocalDate() + RandomUtil.generateNumString(6));
        NativePay.DataBean dataBean = new NativePay.DataBean();
        //授权码（付款方返回的临时授权码，一次有效）
        dataBean.setTradeCode("WAC2B");   // WAC2B 支付宝-主扫支付,    WUC2B 银联二维码-主扫支付
        dataBean.setTradeAmt(1);
        dataBean.setOrderInfo("订单标题");
        dataBean.setInfoAttach("订单描述");

        dataBean.setTimeoutMinutes(300);

        // 清算-延迟结算标识；取值-含义：{“0”:标准不延迟（默认””,”1”:”延迟结算）”}
        // dataBean.setDelaySettleFlag("");
        // 清算-秒到标识；取值-含义：{“0”:”D0清算（前提：开通秒到功能）”,”1”:”T1清算（默认）”}
        // dataBean.setPatnerSettleFlag("1");

        // 清算-分账标识；取值-含义：{“0”:”标准不分账（默认）”,”1”:”分账”} !! 需要分账的交易请上传该字段
         dataBean.setSplitSettleFlag("0");

         // 支付成功后的结果通知地址， 此处替换成自己的地址
         dataBean.setOrgBackUrl("https://notify-test.eycard.cn:7443/WorthTech_PayChannelModule_V1/Channel008/UnionPay_Weixin_Servlet");

        request.setData(dataBean);


        //============================支付宝需要增加下面======================================
        NativePay.AppendDataBean appendData = new NativePay.AppendDataBean();

        TerminalInfo terminalInfo = new TerminalInfo();
        terminalInfo.setLocation("+37.12/-121.213");
//
//        terminalInfo.setTerminalType("11");
//        terminalInfo.setSerialNum("00104206TCA8B3551");
////        terminalInfo.setTerminalIp("192.168.2.1");
//        terminalInfo.setTerminalId("32774788");

        appendData.setTerminalinfo(terminalInfo);
//
        request.setAppendData(appendData);
        //==================================================================

        // 使用商户私钥对data字段加签
        String sign = sign(dataBean, TEST_MER_RSA_PRIVATE_KEY);
        request.setSign(sign);

        String requestStr = JSON.toJSONString(request);

        try {
            String response = HttpConnectUtils.sendHttpSRequest(BASE_URL + "/standard/native", requestStr, "JSON", null);
            System.out.println("\n响应报文：" + response);
            checkSign(response);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * @Description 交易- 标准-交易单笔查询， 商户可以通过查询订单接口主动查询订单状态，完成下一步的业务逻辑。
     * @date: 2021/10/17 9:49
     * @author: pandans
     * 请求示例：
     * {"data":{"oriOrgTrace":"ORG0005320211019095744897746"},"orgId":"ORG000000000053","orgMercode":"531000012972310","orgTermno":"W1489510","orgTrace":"ORG0005320211019095937978998","sign":"mnkwzAQC/wOY0MqMACknLRWEboZLSfstGpC9mSqou+hhHlngq+AwQk5dRfq1ODU5ETA2P/sOOOfgsF8OP20rEJA9TfPjoXNa9psPUlfxdmIcbVWx/Y7nMAMBAg0L2YXxhP0K8mo2U+jV/YkEqwxI7gnZ+mtQ5lrnVDgggz1pM5w=","signType":"SHA256withRSA"}
     * 返回示例：{"sign":"Z5nFiAbSF0GJx5bPn7tIBFks/hHDIQ7xnaeB2RsDYrjDdSUHT6kyX7YO3vOaEWp8EZTzA70Wx3o5zHEvd0ov4/Qo1uZnfsz3vSgYE1mcm2rdd4p9hIs2Z48Qral2sjEzag6eMw3STD45dGensRSfP+3/vQZzkH2+LtAOQJFcMsg=","data":{"finRetcode":"00","tradeCode":"WTB2C","pcTrace":"4200001122202110197912966375","outTrace":"1101450279085019385856","payerOtherInfo":"oUpF8uMtnXAHnm0lrGeedNbGc8gc","timeEnd":"20211019095758","settleAmt":1,"payerAmt":1,"patnerSettleFlag":"1","oriOrgTrace":"ORG0005320211019095744897746"},"sysRetcode":"000000","sysRetmsg":"success","appendData":{"payDetail":{"wxBankType":"PAB_CREDIT"},"disCount":{}}}
     */
    public static void doQuery() {
        TradeQuery request = new TradeQuery();
        request.setOrgId(ORGID);
        request.setOrgMercode(ORGMERCODE);
        request.setOrgTermno(ORGTERMNO);
        request.setSignType(SIGN_TYPE_RSA256);

        // 交易流水，28位定长，规则：orgId 前四位+后四位+yyyyMMddhhmmss+6位自定义 合计28位长度（唯一）
        request.setOrgTrace(ORGID.substring(0, 4) + ORGID.substring(ORGID.length() - 4) + StringUtils.getLocalDate() + RandomUtil.generateNumString(6));

        TradeQuery.DataBean dataBean = new TradeQuery.DataBean();
        //交易流水号
        dataBean.setOriOrgTrace("ORG0000720220224040505517392");

        request.setData(dataBean);
        // 使用商户私钥对data字段加签
        String sign = sign(dataBean, TEST_MER_RSA_PRIVATE_KEY);
        request.setSign(sign);
        String requestStr = JSON.toJSONString(request);

        try {
            String response = HttpConnectUtils.sendHttpSRequest(BASE_URL + "/standard/tradeQuery", requestStr, "JSON", null);
            System.out.println("\n响应报文：" + response);
            checkSign(response);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * @Description 交易 - 标准-关闭订单 ，支持商户对未支付的订单进行关单，易生受理请求后返回关单结果。ps：原交易只能为JSAPI支付,未知交易状态
     * @date: 2021/10/17 9:51
     * @author: pandans
     * 请求：{"data":{"oriOrgTrace":"ORG0005320211019104608985787"},"orgId":"ORG000000000053","orgMercode":"531000012972310","orgTermno":"W1489510","orgTrace":"ORG0005320211019104738532080","sign":"aRzgjAEJ6m5P/cDDVqfGTcNrwSRVj6CiHBkk4F59IYuf/aUTpUbxN4G4mjqgWSjrlphOSChgyg7pMjU5a6B55ds4jHgRFARs+CBIeWcXRvkaUqV8GN0luzZF2XEarK/rTTHbU6nImSjhTLre87UzPDK5W3cYV7wljzZw8KIlV3s=","signType":"SHA256withRSA"}
     * 返回：{"sign":"boSr5roo8JQ192va7C0rKU6ejUdLU8C9QJTtPD4cW51fdKYFdVGFrK98FMcJCwOro2kNv8RkHyVHIzxNdaiipfNK6bZWO28NQA/+wxdCCsBcwROOhDGvldCn0iTn+Xj+XPqHb0FCPnlUHsiryIAAB+NytsCh642tqiiJQihCWNQ=","data":{"finRetcode":"X0","tradeCode":"WTJS1"},"sysRetcode":"000000","sysRetmsg":"success"}
     */
    public static void doCloseTrade() {
        TradeClose request = new TradeClose();
        request.setOrgId(ORGID);
        request.setOrgMercode(ORGMERCODE);
        request.setOrgTermno(ORGTERMNO);
        request.setSignType(SIGN_TYPE_RSA256);

        // 交易流水，28位定长，规则：orgId 前四位+后四位+yyyyMMddhhmmss+6位自定义 合计28位长度（唯一）
        request.setOrgTrace(ORGID.substring(0, 4) + ORGID.substring(ORGID.length() - 4) + StringUtils.getLocalDate() + RandomUtil.generateNumString(6));

        TradeClose.DataBean dataBean = new TradeClose.DataBean();
        //交易流水号
        dataBean.setOriOrgTrace("ORG0005320211019104608985787");

        request.setData(dataBean);
        // 使用商户私钥对data字段加签
        String sign = sign(dataBean, TEST_MER_RSA_PRIVATE_KEY);
        request.setSign(sign);
        String requestStr = JSON.toJSONString(request);

        try {
            String response = HttpConnectUtils.sendHttpSRequest(BASE_URL + "/standard/close", requestStr, "JSON", null);
            System.out.println("\n响应报文：" + response);
            checkSign(response);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * @param notify 支付成功回调
     * @Description 支付成功回调， 请在服务端写相关接口接受PayNotify 参数， 并相应"ok"
     * @date: 2021/10/16 20:17
     * @author: pandans
     */
    public static String doPayNotify(PayNotify notify) {
        // 支付成功回调
        checkSign(JSON.toJSONString(notify));

        return "ok";
    }







}
